import axios from 'axios'
export default {
  namespaced: true, // 分模块的命名空间
  state: {
    loginState: false,
    userid: '',
    token: ''
  },
  mutations: {
    changeLoginState (state, data) {
      state.loginState = data
    },
    changeUserId (state, data) {
      state.userid = data
    },
    changeToken (state, data) {
      state.token = data
    }
  },
  actions: {
    loginAction (context, payload) { // payload 传递过来的参数
      const { loginname, password } = payload
      // 因为执行完登录之后有后续的业务逻辑，所以需要使用promise
      return new Promise((resolve, reject) => {
        axios.post('http://121.89.205.189/api/user/login', { loginname, password }).then(res => {
          if (res.data.code === '10010') {
            resolve('10010')
          } else if (res.data.code === '10011') {
            resolve('10011')
          } else {
            context.commit('changeLoginState', true)
            context.commit('changeUserId', res.data.data.userid)
            context.commit('changeToken', res.data.data.token)
            resolve()
          }
        })
      })
    }
  }
}
